package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.dao.TbShoppInStorehostDao;
import com.thinkgem.jeesite.modules.sys.entity.TbOrder;
import com.thinkgem.jeesite.modules.sys.entity.TbOrderProduct;
import com.thinkgem.jeesite.modules.sys.entity.TbShopProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by jyy on 2016/10/19 0019.
 */
@Service
@Transactional(readOnly = false)
public class TbShopInStorehostService {

    @Autowired
    private TbShoppInStorehostDao tbShoppInStorehostDao;


   public TbOrder queryOrder(Integer id) {
        return tbShoppInStorehostDao.selectByPrimaryId(id);
    }
    public List<TbOrderProduct> queryTbOrderProducts(Integer orderId){
        return tbShoppInStorehostDao.queryListOrderProduct(orderId);
    }
    //修改订单状态
    public void editOrderState(Integer id){
        tbShoppInStorehostDao.editOrderState(id);
    }


    public Page<TbOrder> findReserveByPage(Page<TbOrder> tbOrderPage, TbOrder tbOrder) {
        tbOrder.setPage(tbOrderPage);
        List list = tbShoppInStorehostDao.findReserveByPage(tbOrder);
        tbOrderPage.setList(list);
        return tbOrderPage;
    }
    //根据uid查询得到shop表的id值
    public int selectShopIdByUid(String uid){
        return tbShoppInStorehostDao.selectShopIdByUid(uid);
    }
    //根据商品id和店铺id查询在shop_product中数据是否存在
    public List<TbShopProduct> selectShopProductIsHave(Map map){
        return tbShoppInStorehostDao.selectShopProductIsHave(map);
    }
    //添加到shop_product中的商品
    public void addShopProduct(TbShopProduct tbShopProduct){

        if("3".equals(tbShopProduct.getReserveFlag())){
            tbShopProduct.setReserveFlag(new Byte("1"));
        }else{
            tbShopProduct.setReserveFlag(new Byte("0"));
        }
        tbShoppInStorehostDao.addShopProduct(tbShopProduct);
    }
    //修改shop_product 中的库存数量
    public void editShopProductStoreNum(TbShopProduct tbShopProduct){
        tbShoppInStorehostDao.editShopProductStoreNum(tbShopProduct);
    }
    // 用户
   public Page<TbOrder> findUserReserveByPage(Page<TbOrder> tbOrderPage, TbOrder tbOrder) {
        tbOrder.setPage(tbOrderPage);
       // 设置默认时间范围，默认当前月
      /* if (tbOrder.getBeginDate() == null){
           tbOrder.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
       }
       if (tbOrder.getEndDate() == null){
           tbOrder.setEndDate(DateUtils.addMonths(tbOrder.getBeginDate(), 1));
       }*/
       Date date=tbOrder.getEndDate();
       if(date!=null){
           date.setHours(23);
           date.setMinutes(59);
           date.setSeconds(59);
       }
        List list = tbShoppInStorehostDao.findUserReserveByPage(tbOrder);
        tbOrderPage.setList(list);
        return tbOrderPage;
    }

}
